package com.lc.controller;

import com.lc.mapper.AreaMapper;
import com.lc.mapper.ShopMapper;
import com.lc.pojo.Area;
import com.lc.pojo.Car;
import com.lc.pojo.Shop;
import com.lc.service.AreaService;
import com.lc.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/shop")
@Slf4j
public class ShopController {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopService shopService;
    @Autowired
    private AreaMapper areaMapper;



    // 门店首页（查询页）
    @RequestMapping("/city")
    public String shopCityList(Model model){

        List<Area> areas = areaMapper.queryCityList();
        model.addAttribute("areas", areas);
        return "shop/shopSelect";
    }

    // 根据城市名查询地区名
    @ResponseBody
    @RequestMapping("/area")
    public List<String> shopAreaList(String city){
        return areaMapper.queryAreaList(city);
    }

    // 根据城市，地区名返回门店列表
    @RequestMapping("/toList")
    public String toShopList(HttpServletRequest request, Shop shop, Model model) throws Exception {

        List<Shop> shops= shopMapper.queryShopList(shop.getShop_city(),shop.getShop_area());
        model.addAttribute("shops", shops);
        return "mange/shop";

    }

    // 点击修改按钮，跳转至修改页面
    @RequestMapping("/toEdit/{shop_number}")
    public String toAdd(@PathVariable String shop_number, Model model) throws Exception {

        Shop shop = shopMapper.queryShopByNum(shop_number);
        model.addAttribute("shop", shop);
        return "mange/editShop";
    }

    // 提交门店修改，跳转至门店列表
    @RequestMapping("/update")
    public String updateShop(HttpServletRequest request, Shop shop, Model model) throws Exception {
        Shop shopNew = new Shop();
        BeanUtils.copyProperties(shop,shopNew);
        Boolean succ = shopService.updateShop(shopNew);
        if (succ) {
            String successMsg = URLEncoder.encode("修改成功", "UTF-8");
            List<Shop> shops= shopMapper.queryShopList(shop.getShop_city(),shop.getShop_area());
            model.addAttribute("shops", shops);
            return "mange/shop";
            // return "redirect:/shop/toList?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("修改失败", "UTF-8");
            return "redirect:/shop/toList?successMsg=" + errorMsg;
        }
    }

    // 点击新增门店按钮，跳转至新增页面
    @RequestMapping("/toAdd")
    public String toAdd() throws Exception {
        return "mange/addShop";

    }

    // 提交门店新增，跳转至门店列表
    @RequestMapping("/add")
    public String addShop(HttpServletRequest request, Shop shop, Model model) throws Exception {
        Shop shopNew = new Shop();
        BeanUtils.copyProperties(shop,shopNew);
        Boolean succ = shopService.addShop(shopNew);
        if (succ) {
            String successMsg = URLEncoder.encode("增加成功", "UTF-8");
            List<Shop> shops= shopMapper.queryShopList(shop.getShop_city(),shop.getShop_area());
            model.addAttribute("shops", shops);
            return "mange/shop";
            // return "redirect:/shop/list?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("增加失败", "UTF-8");
            return "redirect:/shop/list?successMsg=" + errorMsg;
        }
    }

    // 删除门店
    @RequestMapping("/delete/{shop_number}")
    public String deleteShop(@PathVariable String shop_number, Model model) throws Exception {
        Shop shop = shopMapper.queryShopByNum(shop_number);
        int succ = shopMapper.deleteShopByNum(shop_number);
        if (succ==1) {
            String successMsg = URLEncoder.encode("删除成功", "UTF-8");
            List<Shop> shops= shopMapper.queryShopList(shop.getShop_city(),shop.getShop_area());
            model.addAttribute("shops", shops);
            return "mange/shop";
            //return "redirect:/shop/list?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("删除失败", "UTF-8");
            return "redirect:/shop/list?successMsg=" + errorMsg;
        }
    }
}
